package com.gerfmarquez.nymph;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.media.AudioTrack;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.gerfmarquez.nymph.music.core.MusicCore;
import com.gerfmarquez.nymph.music.core.PlayNotesThread;
import com.gerfmarquez.nymph.preferences.MusicPreferences;
import com.gerfmarquez.nymph.ui.seekbar.SpawnNoteView;
import com.gerfmarquez.nymph.ui.seekbar.VerticalSeekBar;
import com.gerfmarquez.nymph.ui.seekbar.ViewInflater;
import com.gerfmarquez.nymph.ui.threads.UpdatePlayStateNotesThread;

public class NymphMelodyCreatorActivity extends Activity {
	
	SharedPreferences shpr;
    static final String TAG= "TestNotesDurationActivity";
    byte[][] notesToPlay;
    byte []rhythm;
    LinearLayout secondaryLinearHorizontalLayout;
    Handler handler;
    
    public ArrayList<AudioTrack> audioTracks = new ArrayList<AudioTrack>();
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(customInflate());
        MusicCore.init(this);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    @Override
    public void onStop() {
    	super.onStop();
    	for(AudioTrack aud: audioTracks) {
    		aud.release();
    	}
    }
    
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		// We have only one menu option
		case R.id.preferences:
			// Launch Preference activity
			Intent i = new Intent(NymphMelodyCreatorActivity.this, MusicPreferences.class);
			startActivity(i);
			break;
		}
		return true;
	}
	public void onResume() {
		super.onResume();
		getPrefs();
	}
    
    public void playSongArray(View view) {
//    	notesToPlay = new byte[][]{B,Cs,E,B,F,G};
    	handler = new Handler();
    	rhythm = getNotesRhythm();
    	notesToPlay = MusicCore.getSoundNotesArray(getNotesFromSlideViews());
    	
    	UpdatePlayStateNotesThread uPSN = new UpdatePlayStateNotesThread(this, getVerticalSeekBars(), handler);
		new PlayNotesThread(rhythm, audioTracks, notesToPlay, uPSN).start();
    }
    
    private void getPrefs() {
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

        shpr = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
        try {
        	MusicCore.setBpm(Integer.parseInt(shpr.getString("bpm", "120")));
        } catch(NumberFormatException nfe) {
        	MusicCore.setBpm(120);
        }
    }
    
    private int[] getNotesFromSlideViews() {
    	int notesCount = secondaryLinearHorizontalLayout.getChildCount();
    	int[] musicNotes = new int[notesCount];
    	for(int i = 0; i < notesCount; i++) {
    		LinearLayout individualNote = (LinearLayout)secondaryLinearHorizontalLayout.getChildAt(i);
    		VerticalSeekBar vSeekBar = (VerticalSeekBar)individualNote.findViewById(R.id.noteLevel);
    		
    		musicNotes[i] = vSeekBar.getMusicNote();
    	}
    	return musicNotes;
    }
    private ArrayList<VerticalSeekBar> getVerticalSeekBars() {
    	ArrayList<VerticalSeekBar> vs = new ArrayList<VerticalSeekBar>();
    	int notesCount = secondaryLinearHorizontalLayout.getChildCount();
    	for(int i = 0; i < notesCount; i++) {
    		LinearLayout individualNote = (LinearLayout)secondaryLinearHorizontalLayout.getChildAt(i);
    		VerticalSeekBar vSeekBar = (VerticalSeekBar)individualNote.findViewById(R.id.noteLevel);
    		vs.add(vSeekBar);
    	}
    	return vs;
    }
    private byte[] getNotesRhythm() {
    	int notesCount = secondaryLinearHorizontalLayout.getChildCount();
    	byte[] notesRhythm = new byte[notesCount];
    	for(int i = 0; i < notesCount; i++) {
    		LinearLayout individualNote = (LinearLayout)secondaryLinearHorizontalLayout.getChildAt(i);
    		Button rhythmButton = (Button)individualNote.findViewById(R.id.noteRhythm);
    		
    		notesRhythm[i] = Byte.parseByte(rhythmButton.getText().toString());
    	}
    	return notesRhythm;
    }
    
    
    private LinearLayout customInflate() {
    	
        LinearLayout mainLinearLayout = ViewInflater.getMainLinearLayout(this);
        secondaryLinearHorizontalLayout = (LinearLayout)mainLinearLayout.findViewById(R.id.secondaryLinearLayout);
        
        for(int i = 0; i< 10; i++) {
        	
        	LinearLayout musicNoteLinearVerticalLayout = SpawnNoteView.spawnNoteView(this);
	        
	        secondaryLinearHorizontalLayout.addView(musicNoteLinearVerticalLayout);
        }
        return mainLinearLayout;
    }
    public void changeNoteLengthTap(View view) {
    	Log.d(TAG,"Length button clicked");
    	Button button = (Button)view;
    	if(button.getText().equals("1")) {
    		Log.d(TAG,"Length button clicked: " + "1");
    		button.setText("2");
    	}
    	else if(button.getText().equals("2")) {
    		Log.d(TAG,"Length button clicked: " + "2");
    		button.setText("3");
    	}
    	else if(button.getText().equals("3")) {
    		Log.d(TAG,"Length button clicked: " + "3");
    		button.setText("4");
    	}
    	else if(button.getText().equals("4")) {
    		Log.d(TAG,"Length button clicked: " + "4");
    		button.setText("8");
    	}
    	else if(button.getText().equals("8")) {
    		Log.d(TAG,"Length button clicked: " + "8");
    		button.setText("1");
    	}
    }
    public void changeColor(View view) {
    	VerticalSeekBar vs = getVerticalSeekBars().get(0);
//		VerticalSeekBar vs = verticalSeekBars.get(currentNote);
		Rect bounds = vs.getProgressDrawable().getBounds();
		int progr = vs.getProgress();
		int max = vs.getMax();
		int width = vs.getWidth(); 
		int height = vs.getHeight();

		vs.setProgressDrawable(this.getResources().getDrawable(R.drawable.myprogress));
		vs.getProgressDrawable().setBounds(bounds);
		vs.setMax(max);
		vs.setProgress(1);
		vs.setProgress(progr);
		vs.onSizeChanged(width, height);
		vs.refreshDrawableState();
    }
}